#include "widget.h"
#include "ui_widget.h"

#include <QGuiApplication>
#include <QScreen>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

}

QRect Widget::GetScreenRect()
{
    auto screens = QGuiApplication::screens();
    auto* pPrimaryScreen = QGuiApplication::primaryScreen();

    QRect totalGeo = pPrimaryScreen->geometry();
    qDebug() << "totalGeo:" <<totalGeo;
    auto ratio = pPrimaryScreen->devicePixelRatio();
    qDebug() << "devicePixelRatio:" << ratio;
    totalGeo = QRect(totalGeo.left(), totalGeo.top(),
        totalGeo.width() * ratio, totalGeo.height() * ratio);

    qDebug() << "totalGeo:" <<totalGeo;

    for (auto* pScreen : screens)
    {
        if (pScreen == pPrimaryScreen)
            continue;

        //获取其他屏幕数据
        auto screenRatio = pScreen->devicePixelRatio();
        auto geo = pScreen->geometry();
        geo = QRect(geo.left(), geo.top(),
            geo.width() * screenRatio, geo.height() * screenRatio);
        qDebug() << "geo :" << geo;

        if (totalGeo.isNull())
            totalGeo = geo;
        else
        {
            if (geo.right() > totalGeo.right())
                totalGeo.adjust(0, 0, geo.width(), 0);
            if (geo.bottom() > totalGeo.bottom())
                totalGeo.adjust(0, 0, 0, geo.height());
        }
    }
    qDebug() << "adjusted geo:" << totalGeo;

    return totalGeo;
}

QRect Widget::NewGetScreenRect()
{
    /* 1 2
       3 4 */
    //不分主次屏，仅确定最左上角的起始点，起始点的地方不一定有屏幕
    //起始点的地方不一定有屏幕，存在屏幕分布为2，3的可能性，此时绘图的起始点在1的左上角
    auto screens = QGuiApplication::screens();

    QRect totalRect = QRect();
    for (auto* pScreen : screens)
    {
        auto screenRatio = pScreen->devicePixelRatio();
        auto geo = pScreen->geometry();
        geo = QRect(geo.left(), geo.top(),
            geo.width() * screenRatio, geo.height() * screenRatio);
        qDebug() << "geo :" << geo;

        totalRect = totalRect.united(geo);
    }
    //unitedRect = unitedRect.united(geo);

    //首先确定左上角的起始点，直接找最小值即可
//    int left = 0, top = 0, right = 0, bottom = 0;
//    for (auto geo : geoVector) {
//        //首先确定左上角的起始点，直接找最小值即可
//        if (geo.left() < left) {
//            left = geo.left();
//        }
//        if (geo.top() < top) {
//            top = geo.top();
//        }
//        //win11的屏幕设置允许斜放的屏幕不用角对着角，相加计算得到的宽高也不靠谱，直接用最大值找右下角的终止点
//        if (geo.right() > right) {
//            right = geo.right();
//        }
//        if (geo.bottom() > bottom) {
//            bottom = geo.bottom();
//        }
//    }
//    totalRect = QRect(left, top, right - left, bottom - top);
    qDebug() << totalRect;

    return totalRect;
}

void Widget::SaveImage()
{
    QRect rectGif = NewGetScreenRect();

    QScreen* screen = QApplication::primaryScreen();
    auto pix = screen->grabWindow(0, rectGif.x(), rectGif.y(), rectGif.width(), rectGif.height());
    //pix = pix.copy(rectGif);
    QImage image = pix.toImage().scaled(rectGif.width() / 3, rectGif.height() / 3, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
    QPixmap pixmap = QPixmap::fromImage(image);

    ui->label->setPixmap(pixmap);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    SaveImage();
}

